Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Public Partial Class DbfForm
	Inherits Form
    Private mDbfTable As DbfDotNet.DbfTable
    Private DBF_DT As DbfDotNet.DBFDataTable
    Friend Sub New()
        InitializeComponent()
    End Sub
    Friend Sub New(pathfile As String, ByVal version As DbfDotNet.DbfVersion)
        DBF_DT = New DbfDotNet.DBFDataTable(pathfile, version)
        DBF_DT.Fill()
    End Sub
    Private Sub MyBase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        
    End Sub
    Friend Sub PopulateListView()
        Me.LV_DBFTable = New ListView
        Me.LV_DBFTable.Clear()
        Me.LV_DBFTable.Columns.Add("Record #", 80)
        '
        For Each Column As Data.DataColumn In Me.DBF_DT.Columns
            Me.LV_DBFTable.Columns.Add(Column.ColumnName, 120)
        Next Column
        '
        Dim X As Int32 = 1
        For Each Row As Data.DataRow In Me.DBF_DT.Rows
            Dim LVI As ListViewItem = New ListViewItem
            LVI.Text = X.ToString
            For Y As Int32 = 0 To Me.DBF_DT.Columns.Count - 1
                LVI.SubItems.Add(Row.Item(Y).ToString)
            Next Y
            X += 1
            Me.LV_DBFTable.Items.Add(LVI)
        Next Row
    End Sub
	Public Property DbfTable() As DbfDotNet.DbfTable
		Get
			Return mDbfTable
		End Get
		Set
			If value Is mDbfTable Then
				Return
			End If
			mDbfTable = value
			dbfTableView1.DbfTable = value
			textBox1.Clear()
			While splitContainer1.Panel1.Controls.Count > 0
				splitContainer1.Panel1.Controls(0).Dispose()
			End While
			If value IsNot Nothing Then
                Dim sb As StringBuilder = New StringBuilder()
				sb.AppendLine("using DbfDotNet;")
				sb.AppendLine("")
				sb.AppendLine("namespace yournamespace")
				sb.AppendLine("{")
				sb.AppendLine("   public class yourtable : DbfRecord")
				sb.AppendLine("   {")
				For i As Integer = 0 To mDbfTable.Columns.Count - 1
                    Dim col As DbfDotNet.Core.ColumnDefinition = mDbfTable.Columns(i)
                    Dim colType As String = (If(col.DataType Is Nothing, "unknown", col.DataType.Name))
                    Dim columnName As String = col.ColumnName
					sb.AppendLine(String.Format("      public {0} {1}", colType, columnName))
					Dim column As New DbfColumn()
					column.label1.Text = col.ColumnName
					column.Dock = DockStyle.Top
					splitContainer1.Panel1.Controls.Add(column)

					column.BringToFront()
				Next
				sb.AppendLine("   }")
				sb.AppendLine("}")
				textBox1.Text = sb.ToString()
			End If
		End Set
	End Property

End Class
